<template>
  <Dialog
    :title="t('judgeScene.commonHeader.groupSettings')"
    v-model="dialogVisible"
    width="1200"
    :fullscreen="false"
  >
    <el-descriptions :column="4" direction="vertical" border>
      <el-descriptions-item
        v-for="(item, index) in majorList"
        :key="index"
        :label="item.label"
        width="200"
      >
        <div v-if="!item.selected">
          {{ t('judgeScene.card.closed') }}
        </div>
        <div v-else-if="findMajorGroup(item.value)">
          <!-- 仅博士 -->
          <template v-if="instanceRule?.amountType === 1">
            <div>
              {{ t('stageMajorGroup.fields.phdAmount') }}:
              {{ findMajorGroup(item.value).phdAmount }}
            </div>
          </template>
          <!-- 仅硕士 -->
          <template v-if="instanceRule?.amountType === 2">
            <div>
              {{ t('stageMajorGroup.fields.masterAmount') }}:
              {{ findMajorGroup(item.value).masterAmount }}
            </div>
          </template>
          <!-- 博士硕士并列 -->
          <template v-if="instanceRule?.amountType === 3">
            <div>
              {{ t('stageMajorGroup.fields.phdAmount') }}:
              {{ findMajorGroup(item.value).phdAmount }}
            </div>
            <div>
              {{ t('stageMajorGroup.fields.masterAmount') }}:
              {{ findMajorGroup(item.value).masterAmount }}
            </div>
          </template>
          <!-- 博士硕士合并 -->
          <template v-if="instanceRule?.amountType === 4">
            <div>
              {{ t('stageMajorGroup.fields.totalAmount') }}:
              {{ findMajorGroup(item.value).totalAmount }}
            </div>
          </template>
        </div>
        <div v-else>{{ t('judgeScene.card.unset') }}</div>
        <el-link
          class="mr-[10px]"
          type="primary"
          v-if="findMajorGroup(item.value)"
          :underline="false"
          @click="handleShowJudge(item.value)"
          :disabled="!item.selected"
        >
          {{ t('judgeScene.card.viewJudge') }}
        </el-link>
        <el-link
          type="primary"
          :underline="false"
          @click="handleEditMajor(item.value)"
          :disabled="!item.selected"
        >
          {{ t('judgeScene.card.setup') }}
        </el-link>
      </el-descriptions-item>
    </el-descriptions>
  </Dialog>

  <!-- 专业分组设置 -->
  <StageMajorGroupForm ref="stageMajorGroupFormRef" @success="getInfo" />
  <!-- 查看评委 -->
  <StageJudgeSelect ref="stageJudgeSelectRef" />
</template>

<script setup lang="ts">
import { StageMajorGroupApi, StageMajorGroupVO } from '@/api/awards/stagemajorgroup'
import StageMajorGroupForm from './StageMajorGroupForm.vue'
import { InstanceRuleVO } from '@/api/awards/instancerule'
import StageJudgeSelect from '@/views/awards/stagejudge/components/StageJudgeSelect.vue'
import { LVS } from '@/api/awards/template'

/** 阶段-专业分组设置 列表 */
defineOptions({ name: 'StageMajorGroupSettings' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const dialogVisible = ref(false)
const loading = ref(true) // 列表的加载中

const instanceRule = ref<InstanceRuleVO>()
const majorList = ref<LVS[]>([])
const stageMajorGroup = ref<StageMajorGroupVO[]>([])

// 显示弹窗
const open = (rule: InstanceRuleVO) => {
  // 记录传递进来的规则(初评还是终评)
  instanceRule.value = rule
  // 显示弹窗
  dialogVisible.value = true
  // 解析分组对钩
  majorList.value = JSON.parse(rule.majorGroupSettings ?? [])
  // 查询已有的分组设置
  getInfo()
}

// 查询当前评奖阶段规则设置的分组设置
const getInfo = async () => {
  if (instanceRule.value) {
    loading.value = true
    try {
      stageMajorGroup.value = await StageMajorGroupApi.getStageMajorGroupListByRule(
        instanceRule.value.id
      )
    } finally {
      loading.value = false
    }
  }
}

// 在分组设置中找到指定类型的分组
const findMajorGroup = (type: string) => {
  return stageMajorGroup.value.find((item) => item.majorType === type)
}

// 查看评委
const stageJudgeSelectRef = ref<InstanceType<typeof StageJudgeSelect>>()
const handleShowJudge = (type: string) => {
  const majorGroup = findMajorGroup(type)
  if (!majorGroup) {
    message.error(t('judgeScene.card.unset'))
    return
  }

  if (instanceRule.value) {
    stageJudgeSelectRef.value?.open(
      instanceRule.value.awardsId,
      majorGroup.id,
      instanceRule.value.id
    )
  }
}

// 设置专业分组
const stageMajorGroupFormRef = ref<InstanceType<typeof StageMajorGroupForm>>()
const handleEditMajor = (type: string) => {
  if (instanceRule.value) {
    const majorGroup = findMajorGroup(type)
    stageMajorGroupFormRef.value?.open(instanceRule.value, type, majorGroup)
  }
}

defineExpose({ open })
</script>
